from typing import Any, Annotated
from datetime import timedelta

from fastapi.security import OAuth2PasswordBearer, OAuth2PasswordRequestForm
from fastapi import APIRouter, Depends, HTTPException, status, Path
from sqlalchemy.orm.session import Session
from common.deps import check_jwt_token, get_db
from core.response import response_code
from datetime import datetime
from setting.web import settings
from core.schemas.reservation import ReservationCreate,ReservationDelete,ReservationUpdate
import crud

router = APIRouter()

@router.post("/create", summary="添加新的预约")
async def create_reservation( reservation: ReservationCreate, db: Session = Depends(get_db),payload=Depends(check_jwt_token)):
    res = crud.reservation.create(db,reservation,payload.get("uid"))
    res2 = crud.participant.create(db,res.reservation_id,reservation.participantUsers)
    return response_code.resp_200(data=res)

@router.get("/list", summary="列出预约")
async def list_reservation(page: int = 1, pageSize: int = 20, current:bool = False,db: Session = Depends(get_db),payload=Depends(check_jwt_token)):
    uid = payload.get("uid")
    if current:
        res = crud.reservation.list_reservations(db,page,pageSize,uid)
    else:
        res = crud.reservation.list_reservations(db,page,pageSize)
    return response_code.resp_200(data=res)

@router.get("/check", summary="检查预约")
async def check_reservation(page: int = 1, pageSize: int = 20, db: Session = Depends(get_db),payload=Depends(check_jwt_token)):
    res = crud.classroom.check(db,new_classroom)
    return response_code.resp_200(data=res)

@router.get("/{id}", summary="获取预约")
async def get_reservation(id: Annotated[int, Path(title="预约id编号")], db: Session = Depends(get_db),payload=Depends(check_jwt_token)):
    res = crud.reservation.delete(db,id)
    return response_code.resp_200(data=res)

@router.put("/{id}", summary="更新预约")
async def modify_reservation(id: Annotated[int, Path(title="预约id编号")],new_data: ReservationUpdate, db: Session = Depends(get_db),payload=Depends(check_jwt_token)):
    res = crud.reservation.update_reservation(db,id,new_data)
    return response_code.resp_200(data=res)

@router.delete("/{id}", summary="删除预约")
async def delete_reservation(id: Annotated[int, Path(title="预约id编号")], db: Session = Depends(get_db),payload=Depends(check_jwt_token)):
    res = crud.reservation.remove(db,id)
    return response_code.resp_200(data=res)
